package com.loop.unclelend.ui.presenter

import com.loop.unclelend.app_config.UncleConfigs
import com.loop.unclelend.network.ApiService
import com.loop.unclelend.network.ApiService.addCommonParams
import com.loop.unclelend.network.ApiService.addRandomParams
import com.loop.unclelend.network.IHttpCallback
import com.loop.unclelend.pro.ProPresenter
import com.loop.unclelend.ui.view.IView_Pin_Payment_Status
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/27 13:45
 * @Annotation：支付状态查询 Presenter 实现
 */
class Pre_Pin_Payment_Status(private val iView: IView_Pin_Payment_Status): ProPresenter(iView) {

    //三方订单id
    private var thirdOrderId = ""
    fun cacheThirdOrderId(orderId: String){
        thirdOrderId = orderId
    }

    /**
     * 获取支付状态
     */
    fun requestUnclePinPaymentStatusAction(isStartCountdown: Boolean = true){
        iView.showUncleLoading()
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.UNCLE_API_COMMON_URL)
        val params = JSONObject().addCommonParams().addRandomParams()
        params.put("path",ApiService.API_PATH.UNCLE_API_LOAN_REPAYMENT_STATUS_PATH)
        params.put("thirdOrderId",thirdOrderId)//三方订单id
        doPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                iView.hideUncleLoading()
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    obj.put("isStartCountdown",isStartCountdown)
                    if (obj.has("transactionId")){
                        thirdOrderId = obj.optString("transactionId")
                    }

                    // PROCESSING /SUCCEEDED /FAILED
                    val status = obj.optString("status")
                    when(status){
                        "FAILED" -> {
                            iView.showUnclePinPaymentFailed(obj)
                        }
                        "SUCCEEDED" -> {
                            iView.showUnclePinPaymentSuccess(obj)
                        }
                        else -> {
                            iView.showUnclePinPaymentProcess(obj)
                        }
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
                iView.hideUncleLoading()
            }
        })
    }


    /**
     * 订单还款
     */
    fun requestUncleLoanPayLinkAction(){
        iView.showUncleLoading()
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.UNCLE_API_COMMON_URL)
        val params = JSONObject().addCommonParams().addRandomParams().apply {
            put("orderId",iView.getOrderId())
            put("path",ApiService.API_PATH.UNCLE_API_LOAN_REPAYMENT_URL_PATH)
        }
        if (UncleConfigs.isUgx() || UncleConfigs.isKsh()){//肯尼亚、乌干达 需要使用 getOrderRepayH5 获取链接
            params.put("path",ApiService.API_PATH.UNCLE_API_LOAN_REPAYMENT_KSH_UGX_URL_PATH)
        }
        doPostAction(httpUrl,params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                iView.hideUncleLoading()
                val obj = data.optJSONObject("obj")
                if (obj != null){
                    thirdOrderId = obj.optString("thirdOrderId")
                    requestUnclePinPaymentStatusAction()
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
            }
        })
    }
}